<template>
  <div class="sale-hotel">
    <Breadcrumb :pathContent="content" />
    <div class="sale-hotel-content">
      <el-form>
        <el-form-item>
          <el-dropdown trigger="click"
            @command="dropdownStateChange">
            <span class="el-dropdown-link">
              {{dropdownValue}}
              <i class="el-icon-arrow-down el-icon--right"></i>
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item :command="{value: null, title: '全部状态'}">
                全部状态
              </el-dropdown-item>
              <el-dropdown-item :command="{value: 1, title: '启用'}">
                启用
              </el-dropdown-item>
              <el-dropdown-item :command="{value: 0, title: '禁用'}">
                禁用
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
          <el-dropdown trigger="click"
            @command="_dropdownStateChange">
            <span class="el-dropdown-link">
              {{dropdownVal}}
              <i class="el-icon-arrow-down el-icon--right"></i>
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item :command="{value: '', title: '全部推荐状态'}">
                全部推荐状态
              </el-dropdown-item>
              <el-dropdown-item :command="{value: 1, title: '推荐'}">
                推荐
              </el-dropdown-item>
              <el-dropdown-item :command="{value: 0, title: '非推荐'}">
                非推荐
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
          <div class="area-select">
            <el-cascader :options="cities"
              :props="{ value: 'cityid', label: 'city', children: 'citiess' }"
              @change="handleCascader"
              filterable
              clearable
              placeholder="请选择城市"
              change-on-select>
            </el-cascader>
          </div>

          <div>

            <el-input placeholder="请输入酒店名/酒店ID"
              v-model="searchKey"
              clearable>
            </el-input>

          </div>
          <el-button type="primary"
            @click="searchHotel"
            class="searchHotel">
            <i class="el-icon-search"></i>
          </el-button>
          <el-button type="primary"
            size="small"
            style="float: right; margin-right: 1%;background:#6d2377;border-color:#6d2377;"
            @click="addSaleHotel"
            v-if="addhotel">新增酒店</el-button>
        </el-form-item>
        <el-form-item>
          <el-table :data="tableData"
            style="width: 100%"
            min-height="250"
            max-height="600"
            v-loading="loading"
            @selection-change="handleSelectionChange">
            <el-table-column prop="num"
              label="酒店ID">
            </el-table-column>
            <el-table-column prop="name"
              label="酒店名称">
            </el-table-column>
            <el-table-column prop="province"
              label="所在省份">
            </el-table-column>
            <el-table-column prop="city"
              label="所在城市">
            </el-table-column>
            <el-table-column prop="area"
              label="所在区/县">
            </el-table-column>
            <el-table-column prop="star"
              label="星级">
            </el-table-column>
            <el-table-column prop="orderCount"
              label="客房数">
            </el-table-column>
            <el-table-column prop="state"
              label="状态"
              :formatter="stateFormat"
              v-if="updatehotelstate">
            </el-table-column>
            <el-table-column label="操作"
              width="400">
              <template slot-scope="scope">
                <el-button type="text"
                  size="small"
                  @click="_delete(scope.row.id,scope.$index)">
                  删除
                </el-button>
                <el-button type="text"
                  size="small"
                  @click="handleStateClick(scope.row)">
                  {{scope.row.state === "0"? "启用":"禁用"}}
                </el-button>
                <el-button type="text"
                  size="small"
                  @click="edit(scope.row.id)">
                  编辑
                </el-button>
                <el-button type="text"
                  size="small"
                  @click="jumpRoom(scope.row.id)">
                  房型
                </el-button>
                <el-button type="text"
                  @click="handler(scope.row)">
                  {{scope.row.status ==1 ? "查看推荐理由" : "设为推荐酒店"}}
                </el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-form-item>
        <el-form-item style="margin-top: 20px;">
          <el-pagination @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="pageNum"
            :page-sizes="[10, 20, 30, 40]"
            :page-size="pageSize"
            layout="total, prev , pager , next, sizes, jumper"
            :total="total">
          </el-pagination>
        </el-form-item>
      </el-form>
      <!-- 推荐酒店弹窗 -->
      <el-dialog :visible.sync="dialogVisible"
        width="500px">
        <div class="content">
          <el-form>
            <el-form-item>
              <el-input placeholder="请输入推荐理由"
                v-model="remark"
                type="textarea"
                maxlength="50"
                @keyup.native='remark = remark.replace(/^\s+|\s+$/g,"")'
                :autosize="{ minRows: 4, maxRows: 8}"></el-input>
            </el-form-item>
          </el-form>
        </div>
        <span slot="footer"
          class="dialog-footer">
          <!-- 取消推荐 -->
          <el-button type="primary"
            v-if="isRecommend==1"
            @click="_cancleRecomm">取消推荐</el-button>

          <el-button type="primary"
            @click="_recomm">确 定</el-button>
          <el-button @click="dialogVisible = false">取 消</el-button>

        </span>
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { recomm } from "@/public/js/saleHotel";
import { getHotelList, updateHotelstate } from "@/public/js/hotelInfomation";
const Breadcrumb = () => import("@/components/common/Breadcrumb");
const cityData = require("@/data/cities.json");
export default {
  name: "SaleHotellist",
  data() {
    return {
      state: [],
      status: 0,
      dropdownValue: "全部状态",
      dropdownVal: "全部推荐状态",
      cities: cityData.provincesEntities,
      visible: false,
      content: {
        firstTitle: "系统设置",
        secondTitle: "酒店列表管理",
        type: "close"
      },
      tableData: [], // 列表数据
      selectionArray: [],
      total: 0,
      saleHotelIds: [],
      statusSelect: null, // 酒店状态(默认为全部状态 null)
      statusBtn: null,
      pageSize: 10,
      pageNum: 1,
      city: null,
      searchKey: null, // 酒店搜索关键字(默认为空 null)
      text: "",
      dialogVisible: false,
      isRecommend: false,
      remark: "",
      _status: "",
      hotelId: "",
      loading: true,
      __status: ""
    };
  },
  created() {
    this.getHotelListData();
    window.commo.showMenu(this);
    sessionStorage.setItem("TabName", "first");
    sessionStorage.removeItem("imgsData");
  },
  methods: {
    // 获取酒店列表数据
    getHotelListData() {
      let data = {
        pageSize: this.pageSize,
        pageNum: this.pageNum,
        state: this.statusSelect,
        searchKey: this.searchKey,
        status: this.__status,
        city: this.city
      };
      this.loading = true;
      getHotelList(data).then(res => {
        if (!res.data.errCode) {
          this.tableData = res.data.list;
          this.loading = false;
          this.total = res.data.total; // 获取列表总条数
        }
      });
    },
    //删除酒店
    _delete(id, index) {
      this.$confirm("此操作将永久删除该酒店, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          updateHotelstate({
            state: -1,
            id: id
          }).then(res => {
            console.log(res);
            if (!res.data.errCode) {
              this.tableData.splice(index, 1);
              this.$message.success("删除成功");
            } else {
              this.$message.error(res.data.errMsg);
            }
          });
        })
        .catch(() => {});
    },
    // handler
    handler(row) {
      this.hotelId = row.id;
      this.dialogVisible = true;
      this.isRecommend = row.status == 1;
      this.remark = row.remark;
    },
    // 取消推荐
    _cancleRecomm() {
      this._status = 2;
      this.remark = "";
      this.handleRecomm();
    },
    _recomm() {
      this._status = 1;
      this.handleRecomm();
    },
    handleRecomm() {
      if (!this.remark && this._status == 1) {
        this.$message.error("请输入推荐理由");
        return false;
      }
      recomm({
        hotelId: this.hotelId,
        status: this._status,
        remark: this.remark
      }).then(res => {
        if (!res.data.errCode) {
          this.$message({
            type: "success",
            message: "操作成功"
          });
          this.dialogVisible = false;
          this.getHotelListData();
        }
      });
    },
    // 操作修改酒店状态
    setHotelStates(row) {
      let state = row.state === "0" ? "1" : "0";
      let stateData = {
        state: state,
        id: row.id
      };
      updateHotelstate(stateData).then(res => {
        console.log(res);
        if (!res.data.errCode) {
          this.$message.success("修改成功");
          row.state = row.state === "0" ? "1" : "0";
        } else {
          this.$message.error(res.data.errMsg);
        }
      });
    },
    // 格式化状态
    stateFormat(row, column) {
      let state = row[column.property];
      return state === "1" ? "启用" : "禁用";
    },
    // 修改状态
    handleStateClick(row) {
      // console.log(row.state = 0)
      this.$confirm("此操作将" + this.text + "该酒店, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.setHotelStates(row);
        })
        .catch(() => {});
      // row.state = row.state ? 0 : 1
      // return row.state ? '0' : '1'
    },
    // 查询按城市
    handleCascader(val) {
      this.city = parseInt(val[val.length - 1]);
      this.pageNum = 1;
      this.getHotelListData();
    },
    // 查询按关键字
    searchHotel() {
      this.pageNum = 1;
      this.getHotelListData();
    },
    /** **页码操作start****/
    handleSizeChange(val) {
      this.pageSize = val;
      this.getHotelListData();
    },
    handleCurrentChange(val) {
      this.pageNum = val;
      this.getHotelListData();
    },
    handleSelectionChange(row) {
      this.selectionArray = row;
      this.selectionArray.forEach((item, index) => {
        if (this.saleHotelIds.indexOf(item.id) < 0)
          this.saleHotelIds.push(item.id);
      });
    },
    /** **页码操作end*****/
    // 房型
    jumpRoom(val) {
      // sessionStorage.removeItem('imgsData')
      this.$router.push(`/index/room_list/${val}`);
    },
    // 酒店编辑
    edit(id) {
      // sessionStorage.removeItem('imgsData')
      sessionStorage.setItem("isCreate", false);
      /** 缓存hotelId */
      sessionStorage.setItem("hotelId", id);
      this.$router.push(`/index/edit_hotel/${id}`);
    },
   
    // 增加酒店
    addSaleHotel() {
      // sessionStorage.removeItem('imgsData')
      sessionStorage.setItem("isCreate", true);
      this.$router.push("/index/add_hotel");
    },
    batchState() {
      this.setSaleHotelStates();
      this.visible = false;
    },
    _dropdownStateChange(data) {
      console.log(data);
      this.dropdownVal = data.title;
      this.__status = data.value;
      this.pageNum = 1;
      this.pageSize = 10;
      this.getHotelListData();
    },
    dropdownStateChange(data) {
      this.statusSelect = data.value;
      console.log(this.statusSelect);
      this.pageNum = 1;
      this.getHotelListData();
      this.dropdownValue = data.title;
    }
  },
  components: {
    Breadcrumb
  }
};
</script>
<style lang="scss" scoped>
.sale-hotel-content {
  background-color: white;
  padding: 20px;
  position: relative;
  .el-form {
    .el-form-item {
      .el-form-item__content {
        line-height: 34px !important;
        .el-dropdown {
          float: left;
          margin-right: 20px;
        }
        .el-input {
          float: left;
          width: 300px;
          input {
            height: 34px;
            line-height: 34px;
          }
        }
        .el-pagination {
          text-align: right;
          .el-pagination__jump {
            .el-input {
              float: none !important;
              width: 50px !important;
            }
          }
          .el-input {
            width: 100px;
          }
        }
        .el-table__header,
        .el-table__body {
          width: 100% !important;
        }
      }
    }
  }
}
.expand-trigger div {
  height: 50px !important;
}
.hotel-state {
  text-align: left;
  .el-button {
    font-size: 12px;
    color: #45a2ff;
    border: 1px solid #45a2ff;
    padding: 0 20px;
    line-height: 32px;
  }
}
.area-select {
  float: left;
  line-height: 1;
  width: 200px;
  margin-right: 20px;
  .el-cascader {
    line-height: 1;
    width: 200px;
    .el-input {
      width: 200px !important;
    }
  }
  .el-cascader .el-icon-arrow-down {
    line-height: 1;
  }
}
.searchHotel {
  float: left;
  background: #6d2377;
  border-color: #6d2377;
  margin-left: 15px;
  padding: 9px 21px;
}
.el-popper[x-placement^="bottom"] {
  width: 100px !important;
  padding: 0;
}
.el-popper[x-placement^="bottom"] .el-form-item .el-form-item__content .title {
  background-color: rgb(241, 243, 248);
  text-align: left;
  padding-left: 20px;
  h1 {
    font-size: 14px;
  }
}
.el-popper[x-placement^="bottom"]
  .el-form-item
  .el-form-item__content
  .radio-style {
  padding: 20px;
  text-align: center;
  overflow: hidden;
  .radio-left,
  .radio-right {
    float: left;
    width: 50%;
  }
}
.popver-btn {
  overflow: hidden;
  .btn-left,
  .btn-right {
    width: 50%;
    float: left;
    text-align: center;
    button {
      line-height: 32px;
      padding: 0 20px;
    }
  }
  .btn-right {
    button {
      border-left: 1px solid #d8dce5;
    }
  }
}
</style>
